package com.znf4.controller.look;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.znf4.constants.CommonConst;
import com.znf4.enums.OperationStatusEnum;
import com.znf4.id.IdGenerator;
import com.znf4.look.entity.Look;
import com.znf4.look.server.LookServer;
import com.znf4.utils.OSSFile;

/**
 * 实物预览
 */
@Controller
@RequestMapping("/look")
public class LookController {
	
	private static final Logger LOG = LoggerFactory.getLogger(LookController.class);
	
	@Autowired
	private LookServer lookServer ;
	
	@Autowired
	private IdGenerator idGenerator ;
	

	/**
	 * 函数功能说明 ： 添加活动、公告
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public String add(Look look , Model model , HttpServletRequest request) {
		
		try {
			look.setId(idGenerator.getLookNameNumber());
			String fileName ;
			// 文件上传OSS
			Iterator iter = OSSFile.getFileIterator(request) ;
			if (iter != null ) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				// 获取multiRequest 中所有的文件名
				
				while (iter.hasNext()) {
					// 一次遍历所有文件
					MultipartFile file = multiRequest.getFile(iter.next().toString());	
					// 获取文件后缀
					String type = file.getOriginalFilename().indexOf(".") != -1 ? file.getOriginalFilename()
							.substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length()) : null;
					String fileType = type.toLowerCase();
					if(fileType.equals(".jpg") || fileType.equals(".png") || fileType.equals(".jpeg")){
						fileName  = CommonConst.LOOK_IMG_NUMBER + look.getId() ;
						
						int i = 1 ;
						if(i == 1){
							look.setImg_url_01(OSSFile.upFile(file , fileName));
						}else if(i == 2){
							look.setImg_url_02(OSSFile.upFile(file , fileName));
						}else if(i == 3){
							look.setImg_url_03(OSSFile.upFile(file , fileName));
						}else{
							look.setImg_url_04(OSSFile.upFile(file , fileName));
						}
						i++ ;
					}else if(fileType.equals(".mp4") || fileType.equals(".avi")){
						fileName  = CommonConst.LOOK_VIDEO_NUMBER + look.getId() ;
						look.setVideo_url(OSSFile.upFile(file , fileName));
					}else {
						model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
						return "good/list";
					}
					LOG.info("活动、公告上传图片到oss.....并设置数据库参数。");
				}
			}
			lookServer.addLook(look);
			
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());
		} catch (Exception e) {
			LOG.info(e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		return "good/list";
	}

	
	
	/**
	 * 函数功能说明 ： 修改公告、活动
	 *
	 * @参数： @return
	 * @return String
	 * @author agui
	 * 只润许改 主要内容，图片等不让改，只能删除，从新添加。
	 * @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public String update(Look look , Model model) {

		lookServer.updateLook(look);
		model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());

		return "good/list";
	}
	
	

	/**
	 * 函数功能说明 ： 删除广告
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public String delOne(Look look , Model model) {
		
		try {
			lookServer.delOneLook(look);
			// 删除文件 OSS
			OSSFile.deleteFile(look.getImg_url_01());
			OSSFile.deleteFile(look.getImg_url_02());
			OSSFile.deleteFile(look.getImg_url_03());
			OSSFile.deleteFile(look.getImg_url_04());
			OSSFile.deleteFile(look.getVideo_url());
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc() );
		} catch (Exception e) {
			LOG.info("删除实物预览失败 :", e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		return "good/list";
	}
	
	
	/**
	 * 函数功能说明 ： 删除多条农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public String delAll(String[] looks, Model model) {
		try {
			List<Look> lLook = new ArrayList<>();
			for (int i = 0; i < looks.length; i++) {

				Look look = new Look();
				look.setId(looks[i]);
				lLook.add(look);

				// 删除文件
				Look delLook = lookServer.queryOne(look);
				OSSFile.deleteFile(delLook.getImg_url_01());
				OSSFile.deleteFile(delLook.getImg_url_02());
				OSSFile.deleteFile(delLook.getImg_url_03());
				OSSFile.deleteFile(delLook.getImg_url_04());
				OSSFile.deleteFile(delLook.getVideo_url());

			}
			lookServer.delAllLook(lLook); 
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());
		} catch (Exception e) {
			LOG.info("删除多个实物预览:", e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		return "good/list";
	}

	

}
